package com.example.rageclick;

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

	public SoundPool soundPool;
	public int soundID;
	public boolean somebodyCalled;
	public boolean phoneWentOffHook;
	public boolean soundEnabled;
	boolean loaded = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("RageClick", "Raging...");
		super.onCreate(savedInstanceState);
		this.soundEnabled = true;
		setContentView(R.layout.activity_main);
		initalizePlayer();
		TelephonyManager tm = (TelephonyManager) getSystemService("phone");
		tm.listen(this.phoneListener, 32);
		setVolumeControlStream(AudioManager.STREAM_RING);
		Spinner spinner = (Spinner) findViewById(R.id.spinner);
		// Create an ArrayAdapter using the string array and a default spinner
		// layout
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.sounds, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinner.setAdapter(adapter);
		// Set the on Item Selected Listener of the spinner
		spinner.setOnItemSelectedListener(new SoundsOnItemSelectedListener(this));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private PhoneStateListener phoneListener = new PhoneStateListener() {
		public void playSound() {
			AudioManager audioManager = (AudioManager) MainActivity.this
					.getSystemService("audio");
			int ringerMode = audioManager.getRingerMode();
			float actualVolume = audioManager
					.getStreamVolume(AudioManager.STREAM_RING);
			float maxVolume = audioManager
					.getStreamMaxVolume(AudioManager.STREAM_RING);
			float volume = actualVolume / maxVolume;
			Log.d("RageClick", "About to play sound.");
			if (MainActivity.this.loaded
					&& ringerMode != AudioManager.RINGER_MODE_SILENT
					&& ringerMode != AudioManager.RINGER_MODE_VIBRATE
					&& MainActivity.this.soundEnabled) {
				MainActivity.this.soundPool.play(MainActivity.this.soundID,
						volume, volume, 1, 0, 1.0F);
				Log.d("RageClick", "Played sound.");
			}
		}

		public void onCallStateChanged(int state, String incomingNumber) {
			try {
				Log.d("RageClick", "State: " + state + " Incoming Number: "
						+ incomingNumber);
				if (userEndedCall(state, incomingNumber)) {
					playSound();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		public boolean userEndedCall(int state, String incomingNumber) {
			if (state == TelephonyManager.CALL_STATE_RINGING) {
				MainActivity.this.somebodyCalled = true;
			} else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
				MainActivity.this.phoneWentOffHook = true;
				MainActivity.this.soundPool.stop(MainActivity.this.soundID);
			} else if (MainActivity.this.phoneWentOffHook
					&& !MainActivity.this.somebodyCalled
					&& state == TelephonyManager.CALL_STATE_IDLE) {
				MainActivity.this.somebodyCalled = false;
				MainActivity.this.phoneWentOffHook = false;
				Log.d("RageClick", "IDLE: incoming number is " + incomingNumber);
				Log.d("RageClick",
						"Current State: TelephonyManager.CALL_STATE_IDLE");
				return true;
			}
			return false;
		}
	};

	public void initalizePlayer() {
		// Set the hardware buttons to control the music
		this.soundPool = new SoundPool(10, AudioManager.STREAM_RING, 0);
		this.soundPool
				.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
					public void onLoadComplete(SoundPool soundPool,
							int sampleId, int status) {
						MainActivity.this.loaded = true;
					}
				});
		this.soundID = this.soundPool.load(this, R.raw.punch, 1);
	}

	public void onToggleClicked(View view) {
		this.soundEnabled = ((ToggleButton) view).isChecked();
	}

	public static int getRawVersionOfSelectedSound(String sound) {
		if (sound.equals("Punch")) {
			return R.raw.punch;
		} else if (sound.equals("Torpedo")) {
			return R.raw.torpedo;
		}
		return R.raw.trek;
	}

}
